package com.yuexun.beilunpatient.ui.login.ui;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.ui.bean.BaseListEntity;
import com.yuexun.beilunpatient.ui.login.bean.LoginBean;
import com.yuexun.beilunpatient.ui.login.bean.SmsBean;
import com.yuexun.beilunpatient.ui.login.model.impl.LoginModel;
import com.yuexun.beilunpatient.ui.login.presenter.impl.ChangePasswordPresenter;
import com.yuexun.beilunpatient.ui.login.ui.view.IChangePasswordView;

import org.kymjs.kjframe.ui.ViewInject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/3/19 0019.
 */

public class Act_Forget_Psw extends BaseKJActivity implements IChangePasswordView {

    @Bind(R.id.self)
    RadioButton self;
    @Bind(R.id.kinsfolk)
    RadioButton kinsfolk;
    @Bind(R.id.username_identity)
    EditText usernameIdentity;
    @Bind(R.id.authBt)
    TextView authBt;
    @Bind(R.id.auth_code)
    EditText authCode;
    @Bind(R.id.newPsw)
    EditText newPsw;
    @Bind(R.id.reNewPsw)
    EditText reNewPsw;

    ChangePasswordPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_forget_psw);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new ChangePasswordPresenter(new LoginModel(), this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back, R.id.authBt, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.authBt:
                if(!usernameIdentity.getText().toString().isEmpty()){
                    Map<String, String> params = new HashMap<>();
                    params.put("idCardNO", usernameIdentity.getText().toString());
                    presenter.resetPasswordOfSms(params);
                    /** 倒计时60秒，一次1秒 */
                    CountDownTimer timer = new CountDownTimer(60*1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            // TODO Auto-generated method stub
                            authBt.setText("还剩"+millisUntilFinished/1000+"秒");
                            authBt.setEnabled(false);
                        }

                        @Override
                        public void onFinish() {
                            authBt.setText("获取验证码");
                            authBt.setEnabled(true);
                        }
                    }.start();
                }else{
                    ViewInject.toast("身份证不能为空");
                }
                break;
            case R.id.next:
                if(usernameIdentity.getText().toString().isEmpty()){
                    ViewInject.toast("身份证不能为空！");
                    break;
                }
                if(authCode.getText().toString().isEmpty()){
                    ViewInject.toast("请输入验证码！");
                    break;
                }
                if(newPsw.getText().toString().isEmpty()){
                    ViewInject.toast("请输入密码！");
                    break;
                }
                if(!newPsw.getText().toString().equals(reNewPsw.getText().toString())){
                    ViewInject.toast("两次新密码输入不一致！");
                    break;
                }
                if(checkPwd(newPsw.getText().toString())>=2) {
                    if(newPsw.getText().toString().trim().length()>7){
                        Map<String, String> params = new HashMap<>();
                        params.put("appKey", "Android");
                        params.put("password", newPsw.getText().toString());
                        params.put("verifyCode", authCode.getText().toString());
                        params.put("idCardNO", usernameIdentity.getText().toString());
                        presenter.resetPassword(params);
                    }else{
                        ViewInject.toast("输入字符位数不正确！");
                    }
                }else {
                    ViewInject.toast("请确认含数字、大写字母、小写字母、符号中两种或两种以上");
                }
                break;
        }
    }

    private int checkPwd(String pwd)
    {
        int i = pwd.matches(".*\\d+.*") ? 1 : 0;
        int j = pwd.matches(".*[a-z]+.*") ? 1 : 0;
        int k = pwd.matches(".*[A-Z]+.*") ? 1 : 0;
        int l = pwd.matches(".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*") ? 1 : 0;

        return  i+j+k+l;
    }

    @Override
    public void resetPasswordOfSms(BaseListEntity<SmsBean> entity) {
        if (entity.getStatus().equals("200")) {
            ViewInject.toast(entity.getDatas().get(0).getDescription());
        }else{
            ViewInject.toast(entity.getMessage());
        }
    }

    @Override
    public void resetPassword(BaseListEntity<LoginBean> entity) {
        if (entity.getStatus().equals("200")) {
            if (entity.getDatas().get(0).getCode().equals("1")) {
                finish();
            }else{
                ViewInject.toast(entity.getDatas().get(0).getDescription());
            }
        }
    }
}
